//
//  TableViewController.m
//  UI13_AutoLayout 进阶
//
//  Created by dllo on 16/8/1.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "TableViewController.h"
#define kTableView @"reuse"
#import "MyTableViewCell.h"
#import "SelfSizeTool.h"

@interface TableViewController ()
@property (nonatomic, strong) NSArray *dataArray;


@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArray = [NSArray array];
    self.dataArray = @[@"圣诞快乐分老福克斯;联发科是;来电反馈水电费了; 看电视;联发科都说了;方看电视了; 快乐;收款方;独立思考了 端口是分类考试的", @"付款好了克斯;联发科是;来电反馈水电费了; 看电视;联发科都说了;方看电视了; 快乐;收款方;独立思考了 端口是分类考试的卡萨丁;老福克斯;联发科是;来电反馈水电费了; 看电视;电费了; 看电视;联发科都说了;方看电视了; 快乐;收款方;独立思考了 端口是分类考试的卡萨丁;老福克斯;联发科是;来电反馈水电费了; 看电视;联发科都说了;方看电视了; 快乐;收款方;独立思考了 端口是分类考试的", @"卡萨类考试的卡萨丁;老福克斯;联发科是;来电反馈水电费了; 看电视;联发科都说了;方看电视了; 快乐;收款方;独立思考了 端口是分类考试的"];
    
    // 优先级 数字大的会被先执行 数字小的会被后执行 或者不被执行
    // 1. 常用的优先级分四挡 (1000, 750 , 250, 50)
    // 2. 高优先级会被先执行 根据情况 低优先级可能被舍弃
    // 3. 优先级一般来解决约束冲突的问题
    // 4. 1000的优先级 在Xib SB 中为实线 低于1000的优先级为虚线
    // 5. 我们还可以用优先级做动画效果
    
    
    
    
    // 预估高度
//    self.tableView.estimatedRowHeight = 40;
//    // 系统自动计算高度
//    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    // 根据label内容 计算并赋予tableView
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 自己算的label高度 这种方式求label高度与系统求高度的不同在于 这样可以存储 效率更高 可以省略大分布计算时间
    CGFloat height = [SelfSizeTool selfSizeHeightWithString:self.dataArray[indexPath.row] width:[UIScreen mainScreen].bounds.size.width font:[UIFont systemFontOfSize:17]];
    return height + 0.5f;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableView forIndexPath:indexPath];
//    cell.myLabel.numberOfLines = 0;
//    CGSize *labelSize = [self.dataArray[indexPath.row] boundingRectWithSize:CGSizeMake(self.tableView.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil].size;
    cell.myLabel.text = self.dataArray[indexPath.row];
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
